package com.yanqu.road.logic.pb;

import com.yanqu.road.entity.activity.evefeast.EveFeastBlock;
import com.yanqu.road.entity.activity.evefeast.EveFeastGameData;
import com.yanqu.road.entity.activity.evefeast.EveFeastItem;
import com.yanqu.road.logic.activity.evefeast.EveFeastShowInfo;
import com.yanqu.road.pb.activity.EveFeastProto;

import java.util.ArrayList;
import java.util.List;

public class EveFeastPb {
    public static EveFeastProto.EveFeastGameTemp.Builder parseGameDataMsg(EveFeastGameData gameData) {
        EveFeastProto.EveFeastGameTemp.Builder builder = EveFeastProto.EveFeastGameTemp.newBuilder();
        builder.setTurnId(gameData.getTurnId());
        builder.setRound(gameData.getRound());
        for (List<EveFeastBlock> blocks : gameData.getBlocks()) {
            for (EveFeastBlock block : blocks) {
                builder.addBlock(parseGameBlockTempMsg(gameData, block));
            }
        }
        builder.setRemainScore(gameData.getRemainScore());
        builder.setScore(gameData.getScore());
        for (Integer choseItem : gameData.getChoseItems()) {
            builder.addChoseItem(choseItem);
        }
        for (Integer choseBuff : gameData.getChoseBuffs()) {
            builder.addChoseBuff(choseBuff);
        }
        for (Integer haveBuff : gameData.getHaveBuffs()) {
            builder.addHaveBuff(haveBuff);
        }
        builder.setRemoveTimes(gameData.getRemoveTimes());
        builder.setRefreshItemTimes(gameData.getRefreshTimes());

        builder.addAllHaveItem(parseGameItemListMsg(gameData.getHaveItems()));

        return builder;
    }

    public static EveFeastProto.EveFeastBlockTemp.Builder parseGameBlockTempMsg(EveFeastGameData gameData, EveFeastBlock block) {
        EveFeastProto.EveFeastBlockTemp.Builder builder = EveFeastProto.EveFeastBlockTemp.newBuilder();
        builder.setX(block.getX());
        builder.setY(block.getY());

        EveFeastItem haveItem = gameData.getHaveItem(block.getObjectId());
        if (haveItem != null) {
            builder.setItem(parseGameItemTempMsg(haveItem));
            builder.setScoreEx(haveItem.calcScore());
        }

        return builder;
    }

    public static EveFeastProto.EveFeastItemTemp.Builder parseGameItemTempMsg(EveFeastItem item) {
        EveFeastProto.EveFeastItemTemp.Builder builder = EveFeastProto.EveFeastItemTemp.newBuilder();
        builder.setId(item.getId());
        builder.setItemId(item.getItemId());
        builder.setExScore(item.getExScore() + item.getTemporaryScore());
        builder.setEffectRound(item.getEffectRound());
        builder.setMultiple(item.getMultiple() + item.getTemporaryMultiple());

        return builder;
    }

    public static List<EveFeastProto.EveFeastItemTemp> parseGameItemListMsg(List<EveFeastItem> items) {
        List<EveFeastProto.EveFeastItemTemp> list = new ArrayList<>();
        for (EveFeastItem item : items) {
            list.add(parseGameItemTempMsg(item).build());
        }
        return list;
    }

    public static List<EveFeastProto.EveFeastShowGroupTemp> parseShowGroupMsg(List<List<EveFeastShowInfo>> showListList) {
        List<EveFeastProto.EveFeastShowGroupTemp> resultList = new ArrayList<>();
        for (List<EveFeastShowInfo> eveFeastShowInfos : showListList) {
            EveFeastProto.EveFeastShowGroupTemp.Builder builder = EveFeastProto.EveFeastShowGroupTemp.newBuilder();
            for (EveFeastShowInfo showInfo : eveFeastShowInfos) {
                EveFeastProto.EveFeastShowTemp.Builder temp = EveFeastProto.EveFeastShowTemp.newBuilder();
                temp.setShowType(showInfo.getShowType());
                temp.addAllValue(showInfo.getShowParams());

                EveFeastBlock block = showInfo.getBlock();
                if (block != null) {
                    EveFeastProto.EveFeastBlockTemp.Builder blockTemp = EveFeastProto.EveFeastBlockTemp.newBuilder();
                    blockTemp.setX(block.getX());
                    blockTemp.setY(block.getY());
                    if (showInfo.getItem() != null) {
                        blockTemp.setItem(parseGameItemTempMsg(showInfo.getItem()));
                        blockTemp.setScoreEx(showInfo.getItem().calcScore());
                    }
                    temp.setBlock(blockTemp);
                } else if (showInfo.getItem() != null) {
                    temp.setItem(parseGameItemTempMsg(showInfo.getItem()));
                }
                temp.setIsBuffEffect(showInfo.isBuffEffect());

                if (showInfo.getTriggerBlock() != null) {
                    EveFeastProto.EveFeastBlockTemp.Builder blockTemp = EveFeastProto.EveFeastBlockTemp.newBuilder();
                    blockTemp.setX(showInfo.getTriggerBlock().getX());
                    blockTemp.setY(showInfo.getTriggerBlock().getY());
                    temp.setTriggerBlock(blockTemp);
                }

                builder.addShow(temp);
            }
            resultList.add(builder.build());
        }

        return resultList;
    }

}
